<?php

/**
 * Description of ReviewLogic
 *
 * @author xiepengcheng
 */
class ReviewLogic {

    private $query;

    public function __construct() {
        $this->query = DB::table('review');
    }

    public function getReviewByVideoID($video_id, $per_page) {
        $ret = $this->query->where('video_id', '=', $video_id)->paginate($per_page, array('video_id', 'author', 'rating', 'text', 'ctime'));
        return $ret;
    }
    
    public function getAverageScoreByVideoID($video_id) {
        $ret = DB::table('review')->where('video_id', '=', $video_id)->avg('rating');
        return intval($ret) / 10;
    }

    public function insertReview($video_id, $user_id, $author, $text, $rating) {
        $rate_score = $rating;
        if ($rating > 50) {
            $rate_score = 50;
        }
        $ret = $this->query->insert(
                array(
                    'video_id' => $video_id,
                    'user_id' => $user_id,
                    'author' => $author,
                    'text' => $text,
                    'rating' => $rate_score,
                )
        );

        return $ret;
    }

    public function deleteReviewByID($id) {
        $ret = $this->query->delete($id);
        return $ret;
    }

    public function list_Reviews($per_page) {
        $ret = DB::query('select r.id,r.author,r.text,r.rating,r.status,v.title,r.ctime from review as r,video as v where r.video_id=v.id');
        $orders = Paginator::make($ret, count($ret), $per_page, false);
        //return $orders->page(count($ret),$per_page);
        return $orders;
    }

}

?>
